package com.vhall.component.service.account.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vhall.component.dao.account.AdminMenuMapper;
import com.vhall.component.entity.account.MenuesEntity;
import com.vhall.component.entity.account.vo.AdminMenuListReqVo;
import com.vhall.component.entity.account.vo.AdminMenuListRspVo;
import com.vhall.component.service.account.AdminMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xianglei
 * @date 2021/4/26 16:54
 */
@Service
public class AdminMenuServiceImpl implements AdminMenuService {

    @Autowired
    private AdminMenuMapper menuMapper;


    @Override
    public List<AdminMenuListRspVo> getList(AdminMenuListReqVo reqVo) {

        ArrayList<AdminMenuListRspVo> adminMenuListRspVos = new ArrayList<>();
        List<MenuesEntity> menuesEntities = menuMapper.selectList(Wrappers.<MenuesEntity>lambdaQuery()
                .isNull(MenuesEntity::getDeletedAt));
        AdminMenuListRspVo adminMenuListRspVo;
        for (MenuesEntity menuesEntity : menuesEntities) {
            adminMenuListRspVo = new AdminMenuListRspVo();
            BeanUtils.copyProperties(menuesEntity, adminMenuListRspVo);
            adminMenuListRspVos.add(adminMenuListRspVo);
        }
        if (reqVo.getTree() == null || reqVo.getTree() == 0) {
            return adminMenuListRspVos;
        }

        ArrayList<AdminMenuListRspVo> treeOneList = new ArrayList<>();
        for (AdminMenuListRspVo menuListRspVo : adminMenuListRspVos) {
            if (menuListRspVo.getPid() == 0) {
                List treeList = getTreeList(adminMenuListRspVos, menuListRspVo.getMenuId());
                menuListRspVo.setChildren(treeList);
                treeOneList.add(menuListRspVo);
            }
        }
        return treeOneList;
    }

    /**
     * 获取每个子模块
     *
     * @param
     * @return
     */
    private List<AdminMenuListRspVo> getTreeList(List<AdminMenuListRspVo> lists, Integer pid) {
        List<AdminMenuListRspVo> children = new ArrayList<>();
        lists.forEach(list -> {
            if (pid.equals(list.getPid())) {
                getTreeList(lists, list.getMenuId());
                children.add(list);
            }
        });
        return children;
    }
}
